package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.domain.pojo.LoginInfo;
import com.heima.domain.pojo.User;
import com.heima.mapper.UserMapper;
import com.heima.service.UserService;
import com.heima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public LoginInfo login(User user) {
        User userLogin = userMapper.getUsernameAndPassword(user);
        if (userLogin != null) {
            // 生成JWT令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", userLogin.getId());
            claims.put("username", userLogin.getUsername());
            claims.put("name", userLogin.getName());

            String token = JwtUtils.generateJwt(claims);

            LoginInfo loginInfo = new LoginInfo(userLogin.getId(), userLogin.getUsername(), userLogin.getName(), token);
            return loginInfo;
        }
        return null;
    }

}
